﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.Http;
using Common.Logging;
using Learun.Application.Base.SystemModule;
using Learun.Application.Organization;
using Learun.Application.TwoDevelopment.ZZDT_EC;
using Learun.Loger;
using Learun.Util;
using Learun.Util.Operat;
using log4net.Config;
using Pipelines.Sockets.Unofficial.Arenas;

namespace Learun.Application.Web.AppApi
{
    /// <summary>
    /// 关联关系的接口
    /// </summary>
    [RoutePrefix("api/RelApi")]
    [HandlerApiLogin(FilterMode.Ignore)]
    public class RelApiController : WebApiControllerBase
    {
        #region 模块对象
        private ec_reltypeIBLL ec_reltypeIBLL = new ec_reltypeBLL();
        private ec_relDataBLL ec_relDataBLL = new ec_relDataBLL();
        #endregion


        /// <summary>
        /// 根据关联关系的名字拿关联关系的Id
        /// </summary>
        /// <param name="RelName"></param>
        /// <param name="projId"></param>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetRelDefByName(string RelName, string projId)
        {
            var data = ec_reltypeIBLL.GetList("{\"ProjectId\":\"" + projId + "\", \"RelTypeName\": \"" + RelName + "\"}");
            return Success(data);
        }

        /// <summary>
        /// 增加 编辑对象和对象之间的关系。
        /// 请求的Body中带上Json格式的ec_reldata数据集合
        /// </summary>
        /// <param name="ProjectId"></param>
        /// <param name="RelTypeId"></param>
        /// <param name="ID1"></param>
        /// <param name="ID2"></param>
        /// <param name="seq"></param>
        /// <returns></returns>
        [HttpGet]
        [HandlerApiLogin(FilterMode.Enforce)]
        public IHttpActionResult SaveRelDataBatch(string ProjectId)
        {
            var asyncContent = Request.Content.ReadAsStringAsync().Result;
            try
            {
                var entityCol = asyncContent.ToObject<IEnumerable<ec_relDataEntity>>();

                ec_relDataBLL.AddEntityBatch(ProjectId,ref entityCol);

                return Success("创建成功");
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }

        }

        /// <summary>
        /// 根据主键，删除某条关联关系
        /// </summary>
        /// <param name="ProjectId"></param>
        /// <param name="RelDataId"></param>
        /// <returns></returns>
        [HttpGet]
        [HandlerApiLogin(FilterMode.Enforce)]
        public IHttpActionResult RemoveRelData(string ProjectId, string RelDataId)
        {

            ec_relDataBLL.RemoveEntity(ProjectId, RelDataId);
            return Success("delete成功");
        }

        /// <summary>
        /// 找单一记录
        /// </summary>
        /// <param name="ProjectId"></param>
        /// <param name="RelTypeId"></param>
        /// <param name="ID1"></param>
        /// <param name="ID2"></param>
        /// <param name="seq"></param>
        /// <returns></returns>
        //[HttpGet]
        //[HandlerApiLogin(FilterMode.Ignore)]
        //public IHttpActionResult GetRelData(string ProjectId, string RelTypeId, string ID1, string ID2, int seq = 0)
        //{
        //    var res = ec_relDataBLL.GetRelData(ProjectId, RelTypeId, ID1, ID2, seq);
        //    return Success(res);
        //}

        /// <summary>
        /// 主力查询
        /// </summary>
        /// <param name="ProjectId"></param>
        /// <param name="RelTypeId"></param>
        /// <param name="startPort">1 or 2</param>
        /// <param name="StartID">对应startPort出发的设备ID</param>
        /// <param name="seq"></param>
        /// <returns></returns>
        [HttpGet]
        [HandlerApiLogin(FilterMode.Ignore)]
        public IHttpActionResult GetRelCollection(string ProjectId, string RelTypeId, string ID1 = "", string ID2 = "", int seq = 0)
        {
            if (!string.IsNullOrEmpty(ID1) && !string.IsNullOrEmpty(ID2))
            {
                //查单一记录
                var res = ec_relDataBLL.GetRelData(ProjectId, RelTypeId, ID1, ID2, seq);
                return Success(res);
            }
            else
            {
                var res = ec_relDataBLL.GetRelCollection(ProjectId, RelTypeId, ID1, ID2, seq);
                return Success(res);
            }
        }
    }
}